using System;
using System.Linq;
using System.Threading.Tasks;
using HengTong.Model._Admin;
using HengTong.Model.Db.System;
using WalkingTec.Mvvm.Mvc;
using HengTong.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using TiaoTaoHelper.Wtm.Attributes;
using TiaoTaoHelper.Wtm.Models;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;


namespace HengTong.Areas.SupplyChain.Controllers
{

    [AuthorizeJwtWithCookie]
    public partial class SysWarehouseController(DataSyncService service)
    {
        /// <summary>
        /// 和EAS进行数据同步
        /// </summary>
        /// <returns></returns>
        [HttpGet("Sync")]
        [PreventDuplicateSubmit]
        [ActionDescription("数据同步")]
        public async Task<IActionResult> Sync()
        {
            await service.WarehouseSync();
            return Ok("数据同步开始执行, 请稍后刷新页面查看同步结果");
        }
        
        /// <summary>
        /// 获取库管用户
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetFrameworkUsers")]
        public ActionResult GetFrameworkUsers([FromQuery]Guid orgId)
        {
            var data = DC.Set<SysUserJobPosition>()
                .Where(e => e.JobPosition.Roles.Any(r => r.Role.RoleName == "库管") && e.JobPosition.Organizations.Any(e => e.OrgId == orgId))
                .Select(e => e.User)
                .Distinct()
                .GetSelectListItems(Wtm, x => x.Name);
            return Ok(data);
        }

        [HttpGet("GetSysOrganizations")]
        public ActionResult GetSysOrganizations()
        {
            return Ok(DC.Set<SysOrg>()
                .Where(e => e.IsSupplyChainOrg == true)
                .GetSelectListItems(Wtm, x => x.Name));
        }
    }
}
